"""用户相关Schema定义"""
from marshmallow import Schema, fields, validate, post_load
from flask_babel import gettext as _


class UserSchema(Schema):
    """用户信息Schema"""
    
    id = fields.Integer(dump_only=True, metadata={'description': '用户ID'})
    username = fields.String(
        required=True,
        validate=validate.Length(min=3, max=80),
        metadata={'description': '用户名', 'example': 'john_doe'}
    )
    email = fields.Email(
        required=True,
        metadata={'description': '邮箱地址', 'example': 'john@example.com'}
    )
    first_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '名', 'example': 'John'}
    )
    last_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '姓', 'example': 'Doe'}
    )
    full_name = fields.String(
        dump_only=True,
        metadata={'description': '全名', 'example': 'Doe John'}
    )
    avatar_url = fields.Url(
        allow_none=True,
        metadata={'description': '头像URL', 'example': 'https://example.com/avatar.jpg'}
    )
    is_active = fields.Boolean(
        dump_only=True,
        metadata={'description': '是否激活'}
    )
    is_admin = fields.Boolean(
        dump_only=True,
        metadata={'description': '是否管理员'}
    )
    created_at = fields.DateTime(
        dump_only=True,
        format='iso',
        metadata={'description': '创建时间'}
    )
    updated_at = fields.DateTime(
        dump_only=True,
        format='iso',
        metadata={'description': '更新时间'}
    )
    last_login_at = fields.DateTime(
        dump_only=True,
        format='iso',
        allow_none=True,
        metadata={'description': '最后登录时间'}
    )


class UserCreateSchema(Schema):
    """创建用户Schema"""
    
    username = fields.String(
        required=True,
        validate=validate.Length(min=3, max=80),
        metadata={'description': '用户名', 'example': 'john_doe'}
    )
    email = fields.Email(
        required=True,
        metadata={'description': '邮箱地址', 'example': 'john@example.com'}
    )
    password = fields.String(
        required=True,
        validate=validate.Length(min=6, max=128),
        load_only=True,
        metadata={'description': '密码', 'example': 'password123'}
    )
    first_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '名', 'example': 'John'}
    )
    last_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '姓', 'example': 'Doe'}
    )
    avatar_url = fields.Url(
        allow_none=True,
        metadata={'description': '头像URL', 'example': 'https://example.com/avatar.jpg'}
    )


class UserUpdateSchema(Schema):
    """更新用户Schema"""
    
    email = fields.Email(
        metadata={'description': '邮箱地址', 'example': 'john@example.com'}
    )
    first_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '名', 'example': 'John'}
    )
    last_name = fields.String(
        validate=validate.Length(max=50),
        allow_none=True,
        metadata={'description': '姓', 'example': 'Doe'}
    )
    avatar_url = fields.Url(
        allow_none=True,
        metadata={'description': '头像URL', 'example': 'https://example.com/avatar.jpg'}
    )


class UserQuerySchema(Schema):
    """用户查询Schema"""
    
    page = fields.Integer(
        missing=1,
        validate=validate.Range(min=1),
        metadata={'description': '页码', 'example': 1}
    )
    per_page = fields.Integer(
        missing=10,
        validate=validate.Range(min=1, max=100),
        metadata={'description': '每页数量', 'example': 10}
    )
    search = fields.String(
        allow_none=True,
        metadata={'description': '搜索关键词', 'example': 'john'}
    )
    is_active = fields.Boolean(
        allow_none=True,
        metadata={'description': '是否激活状态筛选'}
    )
    sort = fields.String(
        missing='created_at',
        validate=validate.OneOf(['created_at', 'username', 'email']),
        metadata={'description': '排序字段', 'example': 'created_at'}
    )
    order = fields.String(
        missing='desc',
        validate=validate.OneOf(['asc', 'desc']),
        metadata={'description': '排序方向', 'example': 'desc'}
    ) 